<?php
namespace Roomer\Controller;
use Think\Controller;
class RoommanagerController extends Controller {
    public static $room_type =  array(
        "StandardSingle"=>"标准单人房",
        "StandardDouble"=>"标准双人房",
        "LuxuriousSingle"=>"豪华单人房",
        "LuxuriousDouble"=>"豪华双人房",
    );
    public function index(){
        $this->checkSession();
        $this->category_price();
        $this->display();
    }
    public function category_price(){
        $this->checkSession();
        $categories=M('room');
        $StandardSingle=$categories->where("room_category='标准单人房'")->field('room_price')->select();
        $StandardDouble=$categories->where("room_category='标准双人房'")->field('room_price')->select();
        $LuxuriousSingle=$categories->where("room_category='豪华单人房'")->field('room_price')->select();
        $LuxuriousDouble=$categories->where("room_category='豪华双人房'")->field('room_price')->select();
        $this->assign('StandardSingle',$StandardSingle[0]{'room_price'});
        $this->assign('StandardDouble',$StandardDouble[0]{'room_price'});
        $this->assign('LuxuriousSingle',$LuxuriousSingle[0]{'room_price'});
        $this->assign('LuxuriousDouble',$LuxuriousDouble[0]{'room_price'});
    }

    public  function edit_roomprice(){//修改房价
        $this->checkSession();
        $StandardSingle=$_POST['StandardSingle'];
        $room=M('room');
        $data['room_price']=$StandardSingle;
        $condition['room_category']='标准单人房';
        $result1=$room->where($condition)->save($data);
        //dump($result1);

        $StandardDouble=$_POST['StandardDouble'];
        $data['room_price']=$StandardDouble;
        $condition['room_category']='标准双人房';
        $result2=$room->where($condition)->save($data);
        //dump($result2);

        $LuxuriousSingle=$_POST['LuxuriousSingle'];
        $data['room_price']=$LuxuriousSingle;
        $condition['room_category']='豪华单人房';
        $result3=$room->where($condition)->save($data);
        //dump($result3);


        $LuxuriousDouble=$_POST['LuxuriousDouble'];
        $data['room_price']=$LuxuriousDouble;
        $condition['room_category']='豪华双人房';
        $result4=$room->where($condition)->save($data);
        //dump($result4);

        if($result1!==false&&$result2!=false&&$result3!==false&&$result4!==false){
            $this->success('修改房价成功');
        }else{
            $this->error('修改房价成功');
        }

    }
    public function edit_roomcategory(){//修改房间类型
        $this->checkSession();
        $room_id=$_POST['room_id'];//获取房间号
        $category=self::$room_type[$_POST['categories']];//解析房间类型
        $data['room_category']=$category;//封装房间类型
        $record=M('room')->where("room_id='$room_id'")->count();
        if(!empty($record)) {
            if ($category !== NULL && $room_id !== '101' && $room_id !== '102' && $room_id !== '103' && $room_id !== '104') {
                $condition['room_id'] = $room_id;//where条件
                $room = M('room');
                $result = $room->where($condition)->save($data);//更新数据
                if ($result !== false) {
                    //$this->success('房间类型已修改');

                    $price = $room->where("room_category='$category'")->field('room_price')->select();//搜索出该类型的房价
                    $data['room_price'] = $price[0]{'room_price'};//取出第一个该类型房价

                    $condition['room_id'] = $room_id;

                    $result1 = $room->where($condition)->save($data);
                    if ($result1 !== false) {
                        $this->success('房间类型已修改');
                    } else {
                        $this->error('房间类型未修改');
                    }
                } else {
                    $this->error('房间修改类型异常');
                }
            } else {
                $this->error('房间类型未修改');
            }
        }else{
            $this->error('暂无该房间号');
        }
    }
    public function empty_room(){
        $this->checkSession();
        if (IS_POST) {
            $startDate = $_POST['startDate'];//提取用户名
            $endDate = $_POST['endDate']; // 提取密码并进行加密
            // $category = self::$room_type[$_POST['category']];
            /*dump($startDate);
            dump($endDate);*/
            $StandardSingle="标准单人房";
            $StandardDouble="标准双人房";
            $LuxuriousSingle="豪华单人房";
            $LuxuriousDouble="豪华双人房";

            //标准单人房
            $StandardSingle_wait_order =M('room_order')->where("endtime='$endDate'and starttime='$startDate'and order_status='0'and category='$StandardSingle'")->count();//等待付款
            $StandardSingle_pay_order =M('room_order')->where("endtime='$endDate'and starttime='$startDate'and order_status='1'and category='$StandardSingle'")->count();//已付款
            $StandardSingle_all =M("room")->where("room_category='$StandardSingle'")->count();
            $StandardSingle_sum=$StandardSingle_all-$StandardSingle_wait_order-$StandardSingle_pay_order;
            //dump($StandardSingle_sum);
            $this->assign('StandardSingle_sum',$StandardSingle_sum);

            //标准双人房
            $StandardDouble_wait_order =M('room_order')->where("endtime='$endDate'and starttime='$startDate'and order_status='0'and category='$StandardDouble'")->count();
            $StandardDouble_pay_order =M('room_order')->where("endtime='$endDate'and starttime='$startDate'and order_status='1'and category='$StandardDouble'")->count();
            $StandardDouble_all =M("room")->where("room_category='$StandardDouble'")->count();
            $StandardDouble_sum=$StandardDouble_all-$StandardDouble_wait_order-$StandardDouble_pay_order;
            //dump($StandardDouble_sum);
            $this->assign('StandardDouble_sum',$StandardDouble_sum);

            //豪华单人房
            $LuxuriousSingle_wait_order =M('room_order')->where("endtime='$endDate'and starttime='$startDate'and order_status='0'and category='$LuxuriousSingle'")->count();
            $LuxuriousSingle_pay_order =M('room_order')->where("endtime='$endDate'and starttime='$startDate'and order_status='1'and category='$LuxuriousSingle'")->count();
            $LuxuriousSingle_all =M("room")->where("room_category='$LuxuriousSingle'")->count();
            $LuxuriousSingle_sum=$LuxuriousSingle_all-$LuxuriousSingle_wait_order-$LuxuriousSingle_pay_order;
            //($LuxuriousSingle_sum);
            $this->assign('LuxuriousSingle_sum',$LuxuriousSingle_sum);

            //豪华双人房
            $LuxuriousDouble_wait_order =M('room_order')->where("endtime='$endDate'and starttime='$startDate'and order_status='0'and category='$LuxuriousDouble'")->count();
            $LuxuriousDouble_pay_order =M('room_order')->where("endtime='$endDate'and starttime='$startDate'and order_status='1'and category='$LuxuriousDouble'")->count();
            $LuxuriousDouble_all =M("room")->where("room_category='$LuxuriousDouble'")->count();
            $LuxuriousDouble_sum=$LuxuriousDouble_all-$LuxuriousDouble_wait_order-$LuxuriousDouble_pay_order;
            //dump($LuxuriousDouble_sum);
            $this->assign('LuxuriousDouble_sum',$LuxuriousDouble_sum);

            $this->display();
        }}
    public  function allroom(){
        $this->checkSession();
        $record = M('room');
        $order =$record->count();
        $per = 12;
        import('Org.Util.Page');
        $page = new \Page($order,$per);
        $sql = "select * from hotel_room ".$page->limit;//显示订单入住时间在今天以及之后的
        $query = $record -> query($sql);
        $pageList=$page->fpage();
        $this->assign('param',$query);
        $this->assign('pageList',$pageList);
        $this->assign('num',1);
        $this->display();
    }
    public function delete_room_id(){
        $this->checkSession();
        $room_id = $_GET['room_id'];
        $room = M('room');
        if($room_id!='101'&&$room_id!='102'&&$room_id!='103'&&$room_id!='104') {
            //执行删除操作
            $room->where("room_id =$room_id")->delete();
            $this->success('删除成功', U('roommanager/allroom'));
        }else{
            $this->error('删除房间异常！请重试');
        }
    }
    public function add_rooms(){//新增房间号
        $this->checkSession();
        $room_id=$_POST['room_id'];
        $room_category=self::$room_type[$_POST['categories']];
        $room=M('room');

        $data['room_id']=$room_id;
        $data['room_category']=$room_category;//封装房间类型



        if($room_category!==NULL) {
            $record = $room->where("room_id='$room_id'")->count();
            if ($record == 0) {
                $room_price = $room->where("room_category='$room_category'")->field('room_price')->select();

                $price = $room_price[0]{'room_price'};

                $data['room_price']=$price;
                if ($room->add($data)) {
                    $this->success('添加客房成功',U('roommanager/allroom') );
                } else {
                    $this->error('发生未知错误，添加用户失败');
                }
            } else {
                $this->error('该房间号已存在，请重试' );

            }}
        else{
            $this->error('房间类型不能为空');

        }
    }
    public function room_show(){
        $this->checkSession();
        $record = M('room_show');
        $order =$record->count();
        $per = 12;
        import('Org.Util.Page');
        $page = new \Page($order,$per);
        $sql = "select * from hotel_room_show ".$page->limit;//显示订单入住时间在今天以及之后的
        $query = $record -> query($sql);
        $pageList=$page->fpage();
        $this->assign('param',$query);
        $this->assign('pageList',$pageList);
        $this->assign('num',1);
        $this->display();
    }
    public function  edit_room_show(){
        $this->checkSession();
        $this->editDisplay();
        $this->display();
    }

    public function editDisplay()
    {
        $this->checkSession();
        $room_category = $_GET['room_category'];
        $info = M('room_show')->where("room_category='$room_category'")->find();
        //$this->assign('admin',$admin);
        $this->assign('info', $info);

    }
    public function  edit_roomshows(){
        $room_category=$_POST['room_category'];
        $this->checkSession();

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        $upload->saveName = time().'_'.mt_rand();
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            $path = '/hotel/Uploads/'.$info['photo']['savepath'].$info['photo']['savename'];
            $data['room_category']=$room_category;
            $data['room_description']=$_POST['room_description'];
            $data['room_pictures']=$path;
            $result=M('room_show')->where("room_category='$room_category'")->save($data);
            if($result!==false){
                $this->success('修改成功',U('roommanager/room_show'));
            }else{
                $this->error('修改失败');
            }
        }

    }
    public function deleteSelect(){
        $this->checkSession();
        $getId = $_POST['id'];
        if(!$getId)
            $this->error('未选择记录');

        $getids = implode(',', $getId); //选择一个以上，就用,把值连接起来(1,2,3)这样

        $id = is_array($getId) ? $getids : $getId; //如果是数组，就把用,连接起来的值覆给$id,否则就覆获取到的没有,号连接起来的值
        D("user")->execute('DELETE FROM hotel_room where room_id IN ('.$id .')');
        $this->success('删除成功');

    }
    public function checkSession(){
        $roomer_id = $_SESSION['roomer_id'];
        if(isset($roomer_id)){
            $this->assign('roomer_id',$roomer_id);
        }else{
            $this->error("需先登录,方可进行查询！",U('Login/index'));
        }
    }

}

























